/*
 * @Description:
 * @Author: qingyang
 * @Date: 2023-06-08 15:26:48
 * @LastEditors: qingyang
 * @LastEditTime: 2023-06-15 10:42:05
 */
import { ref } from 'vue';
import { getFlowHistory } from '@/apis/todo/flow';
import { ProcessStatusEnums } from '@/constants/enum';
export const useHistory = (processInstanceId: string) => {
  const loading = ref(false);
  const progressList = ref([] as any[]);
  const renderIcon = (status: string) => {
    switch (status) {
      case ProcessStatusEnums.PASS:
        return 'c-mian-zhengque';
      case ProcessStatusEnums.REJECT:
        return 'c-mian-qingkong';
      case ProcessStatusEnums.TERMINATE:
        return 'c-mian-tishi';
      default:
        return '';
    }
  };
  const renderStatus = (status: string) => {
    switch (status) {
      case ProcessStatusEnums.PASS:
        return 'SUCCESS';
      case ProcessStatusEnums.REJECT:
        return 'ERROR';
      case ProcessStatusEnums.TERMINATE:
        return 'WARNING';
      default:
        return '';
    }
  };
  const renderSubName = (status: string) => {
    switch (status) {
      case ProcessStatusEnums.PASS:
        return '通过';
      case ProcessStatusEnums.REJECT:
        return '驳回';
      case ProcessStatusEnums.TERMINATE:
        return '终止';
      default:
        return '';
    }
  };
  const loadHistory = async () => {
    loading.value = true;
    try {
      const list = await getFlowHistory(processInstanceId);
      progressList.value = list.map((item: any) => {
        return {
          name: item.activityName,
          date: item.endTime,
          subTitle: item.actionUserName ?? '',
          remark: item.actionComment ? `审核意见：${item.actionComment}` : '',
          icon: renderIcon(item.action),
          status: renderStatus(item.action),
          subName: renderSubName(item.action)
        };
      });
    } finally {
      loading.value = false;
    }
  };

  return {
    loadHistory,
    progressList,
    loading
  };
};
